from symbide import ModuleManager, LogManager

def _to_mathml(objects, expr):
    if ModuleManager.hasModule('sympy'):
        import sympy
        buf = sympy.printing.mathml(expr)
        objects.append("<math xmlns='http://www.w3.org/1998/Math/MathML'>"+buf+"</math>")
        print '__objects__[%s]\n'%str(len(objects)-1)
    elif ModuleManager.hasModule('src.extensions.mathml.mathdom.mathdom'):
        from mathdom.mathdom import MathDOM
        from StringIO  import StringIO
        expr = str(expr).replace('**','^')
        m = MathDOM.fromString(expr, 'infix_term_list')
        buf = StringIO()
        m.toMathml(buf)
        objects.append(buf.getvalue())
        buf.close()
        print '__objects__[%s]\n'%str(len(objects)-1)
    else:
        LogManager.alert("MathMl printing not possible. It seems that neither sympy nor mathdom are available.")